package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ApArticleServiceImpl  implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    /**
     * 加载首页
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto,Integer type) {
        //1.参数校验
        if(dto.getSize()==null){
            dto.setSize(10);
        }
        if(dto.getTag()==null){
            dto.setTag("__all__");
        }
        if(dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
        if(dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }


        //2.调用mapper接口
        List<ApArticle> articleList = apArticleMapper.load(dto, type);

        //3.封装数据
        return   ResponseResult.okResult(articleList);
    }

    /**
     * 新增文章三剑客
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult saveAparticle(ArticleDto dto) {
        ApArticle apArticle=new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        //1.判断是否保存还是修改
        if(dto.getId()==null){
            //如果保存的话
            //新增ap_article
            apArticle.setCreatedTime(new Date());
            apArticle.setPublishTime(new Date());
            apArticleMapper.insert(apArticle);
            //新增ap_article_config
            ApArticleConfig aparticleConfig=new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(aparticleConfig);
            //新增ap_article_content
            ApArticleContent apArticleContent=new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        }else {
            //如果是修改的话
            //修改ap_article
            apArticleMapper.updateById(apArticle);
            //修改ap_article_content
            ApArticleContent apArticleContent=apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId,apArticle.getId()));
            if(apArticleContent!=null){
                apArticleContent.setContent(dto.getContent());
                apArticleContentMapper.updateById(apArticleContent);
            }
        }
        //注意。一定要返回一个文章id，因为我们在文章审核通过之后，要把该articleId写入wmnews表中
        return ResponseResult.okResult(apArticle.getId());
    }


}
